package com.yyfax.ph.common.scan.bankcard.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.yyfax.ph.R;
import com.yyfax.ph.common.scan.Utils.ViewUtil;
import com.yyfax.ph.common.scan.idcard.CameraManager;

/**
 * 项目名称：YYLending
 * 类描述：
 * 创建人：lenovo
 * 创建时间：2016/11/22 18:31
 * 修改人：lenovo
 * 修改时间：2016/11/22 18:31
 * 修改备注：
 */
public class CardView extends View {
    private static final long ANIMATION_DELAY = 10L;


    private int lineStep = 0;
    private Rect mlineRect;
    private Drawable lineDrawable;
    private int LINE_HEIGHT = 0;
    private int LINE_SPEED = 5;
    private final Paint paint;
    private final int frameColor;
    private int left = (int) this.getResources().getDimension(R.dimen.dp_x217);
    private int right = (int) this.getResources().getDimension(R.dimen.dp_x350);

    private Drawable tipsImageDrawable;
    private Drawable backImageDrawable;

    public CardView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint();
        Resources resources = getResources();
        frameColor = R.color.viewfinder_frame;//resources.getColor(ViewUtil.getResourseIdByName(context.getPackageName(), "color", "viewfinder_frame"));


        mlineRect = new Rect();
        int lineDrawableId = ViewUtil.getResourseIdByName(context.getPackageName(), "drawable", "scan_line_portrait");
        lineDrawable = context.getResources().getDrawable(lineDrawableId);
        int tipsImageDrawableId = ViewUtil.getResourseIdByName(context.getPackageName(), "drawable", "bank_card_forground");
        tipsImageDrawable = context.getResources().getDrawable(tipsImageDrawableId);
        int backImageDrawableId = ViewUtil.getResourseIdByName(context.getPackageName(), "drawable", "exit_scan");
        backImageDrawable = context.getResources().getDrawable(backImageDrawableId);
        LINE_HEIGHT = lineDrawable.getIntrinsicHeight();
    }


    @Override
    public void onDraw(Canvas canvas) {
        Rect frame = CameraManager.get().getFramingRect();
        if (frame == null) {
            return;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int lineLeft = left;
        int lineRight = width - right;
        if ((lineStep += LINE_SPEED) < height - LINE_HEIGHT) {
            canvas.save();
            mlineRect.set(lineLeft, lineStep, lineRight, lineStep + LINE_HEIGHT);
            lineDrawable.setBounds(mlineRect);
            lineDrawable.draw(canvas);
            canvas.restore();
        } else {
            lineStep = 0;
        }
        drawTipsImage(canvas);
        drawBackImg(canvas);
        int lineWidth = 10;
        int roundWidth = (frame.right - frame.left) / 20;
        canvas.save();


        paint.setColor(frameColor);

        canvas.drawRect(0 + left, 0, 0 + left + roundWidth, 0 + lineWidth, paint);
        canvas.drawRect(0 + left, 0, 0 + left + lineWidth, 0 + roundWidth, paint);

        canvas.drawRect(width - roundWidth - right, 0, width - right, 0 + lineWidth, paint);
        canvas.drawRect(width - lineWidth - right, 0, width - right, 0 + roundWidth, paint);

        canvas.drawRect(0 + left, height - lineWidth, 0 + left + roundWidth, height, paint);
        canvas.drawRect(0 + left, height - roundWidth, 0 + left + lineWidth, height, paint);

        canvas.drawRect(width - roundWidth - right, height - lineWidth, width - right, height, paint);
        canvas.drawRect(width - lineWidth - right, height - roundWidth, width - right, height, paint);

        canvas.restore();
        postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom);
    }

    private void drawTipsImage(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int left;
        int right;
        int top;
        int bottom;
        int imageWidth = tipsImageDrawable.getIntrinsicWidth();
        int imageHeight = tipsImageDrawable.getIntrinsicHeight();
        left = width - (this.right / 2) - (imageWidth / 2);
        right = left + imageWidth;
        top = 0;
        bottom = imageHeight;
        tipsImageDrawable.setBounds(left,
                top, right,
                bottom);
        tipsImageDrawable.draw(canvas);
    }


    private void drawBackImg(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int left;
        int right;
        int top;
        int bottom;
        int imageWidth = backImageDrawable.getIntrinsicWidth();
        int imageHeight = backImageDrawable.getIntrinsicHeight();

        left = width - (this.right / 2) - (imageWidth / 2);
        right = left + imageWidth;
        top = (height / 2) - (imageHeight / 2);
        bottom = top + imageHeight;
        backImageDrawable.setBounds(left,
                top, right,
                bottom);
        backImageDrawable.draw(canvas);
    }
}